FROM php:7.4-cli
# Set working dir.
ARG CONTAINER_APP_DIR
WORKDIR ${CONTAINER_APP_DIR}

RUN uname -a && \
    apt update

# Install php ext.
RUN apt-get remove -y libssl1.1 && \
    apt-get install libssl1.1 krb5-locales libkrb5-3 libgssapi-krb5-2 \
    libcurl4 libfreetype6-dev zlib1g-dev libwebp-dev libpng-dev libxpm-dev libpcre3-dev libjpeg62-turbo-dev --allow-downgrades -y && \
    docker-php-ext-install sockets pcntl && \
    docker-php-ext-install pdo_mysql && \
    docker-php-ext-configure opcache --enable-opcache && \
    docker-php-ext-install opcache && \
    docker-php-ext-configure gd --with-webp=/usr/include/webp --with-freetype=/usr/include/freetype2 --with-jpeg=/usr/include && \
    docker-php-ext-install -j$(nproc) gd && \
    pecl install redis && \
    docker-php-ext-enable redis && \
    docker-php-ext-enable opcache && \
    apt-get install libssl-dev -y

ARG INSTALL_LIB_EVENT
RUN if [ ${INSTALL_LIB_EVENT} = true ]; then \
    apt-get install libevent-dev -y && \
    pecl install event && \
    echo extension=event > /usr/local/etc/php/conf.d/event.ini \
;fi

RUN docker-php-source delete

# opcache
RUN cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini
RUN sed -i 's/;opcache.enable=1/opcache.enable=1/' /usr/local/etc/php/php.ini
RUN sed -i 's/;opcache.enable_cli=0/opcache.enable_cli=1/' /usr/local/etc/php/php.ini

# Install composer and set mirror.
# Reference: https://pkg.phpcomposer.com/#how-to-install-composer
# Referencr: https://developer.aliyun.com/composer
ARG INSTALL_COMPOSER
ARG COMPOSER_INSTALLER
ARG COMPOSER_MIRROR
RUN if [ ${INSTALL_COMPOSER} = true ]; then \
    php -r "copy('${COMPOSER_INSTALLER}', 'composer-setup.php');" && \
    php composer-setup.php && \
    php -r "unlink('composer-setup.php');" && \
    mv composer.phar /usr/local/bin/composer && \
    composer config -g repo.packagist composer ${COMPOSER_MIRROR} \
;fi

# Expose port
ARG CONTAINER_PORT
EXPOSE ${CONTAINER_PORT}